﻿import java.util.Random;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

/*
 * วิธีการ Add commons-codec-1.7.jar
 * 1. คลิกขวาที่ Project ของเรา แล้วเลือก Properties
 * 2. เลือกเมนูด้านซ้ายมือไปที่ Java Build Path
 * 3. เลือก Tab ที่เขียนว่า Libraries
 * 4. เลือก Add Jar
 * 5. เลือก commons-codec-1.7.jar (อยู่ใน Folder lib)
 * 6. กด OK 
 */

public class AESEncryptDecrypt {
	
	//================================================================================
    // Properties
    //================================================================================

	private Cipher cipherAES;
    private KeyGenerator kgen;
    private SecretKeySpec skeySpec;
    private byte[] rawKey = null;
    private IvParameterSpec ivspec = null;
    
    static Random rnd = new Random();
    
    //================================================================================
    // Constructors
    //================================================================================
    
    public AESEncryptDecrypt() {
        try {
            cipherAES = Cipher.getInstance("AES/CBC/PKCS5Padding");
            
            kgen = KeyGenerator.getInstance("AES");
            kgen.init(256);
            
            byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            ivspec = new IvParameterSpec(iv);
            
        } catch (Exception ex) {
        	ex.printStackTrace();
        } 
    }
    
    //================================================================================
    // Method
    //================================================================================
	
	// สร้าง Secret Key AES
    public void generateKeyAES() throws Exception {
        SecretKey aeskey = kgen.generateKey();
        rawKey = aeskey.getEncoded();
    }
    
    // นำข้อความเข้ารหัส AES
    public String encryptMessage(String message) throws Exception {
        String cypheredtext="";
        
        skeySpec = new SecretKeySpec(rawKey, "AES");
    	cipherAES.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec);
        byte[] clearbyte = message.getBytes();
        
        byte[] cyphered = cipherAES.doFinal(clearbyte);
        cypheredtext = new String(Base64.encodeBase64(cyphered));
        
        return cypheredtext;
    }
    
    // ถอดรหัสข้อความ AES
    public String decrypt(String encryptedMessage, byte[] SecretKeyAESReceive) throws Exception {
    	SecretKey AESKey = new SecretKeySpec(SecretKeyAESReceive, "AES");
        
    	cipherAES.init(Cipher.DECRYPT_MODE, AESKey, ivspec);
    	byte[] clearbyte = Base64.decodeBase64(encryptedMessage);
        byte[] original = cipherAES.doFinal(clearbyte);
        
        return new String(original);
    }
    
    //================================================================================
    // Accessors
    //================================================================================
    
    // รับค่า Public Key AES
    public SecretKey getMyPublicKey() {
        return new SecretKeySpec(rawKey, "AES");
    }
}
